#include<iostream>
#include<thread>
#include<future>
#include<chrono>

int Add(int num1, int num2)
{
    std::cout << "add 111" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(5));

    std::cout << "add 222" << std::endl;
    return num1 + num2;
}


int main()
{   
    //std::async(func, .....)   std::async(policy, .....)
    std::cout << "--------------------1--------------------" << std::endl;

    //std::launch::async：在调用async就开始创建线程。
    //std::launch::deferred：延迟加载方式创建线程。调用async时不创建线程，
    //直到调用了future的get或者wait时才创建线程。

    //std::future<int> restult = std::async(std::launch::deferred, Add, 22, 33);


    std::future<int> restult = std::async(std::launch::async, Add, 22, 33);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "--------------------2--------------------" << std::endl;

    //获取异步结果
    int sum = restult.get();
    
    std::cout << "--------------------3--------------------" << std::endl;
    std::cout << sum << std::endl;
    return 0;
}